using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;

namespace FSC.HaNamAPApplication
{
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class HNAP_Services : System.Web.Services.WebService
    {

        public HNAP_Services()
        {

        }

        [WebMethod]
        public string Hello()
        {
            return "I am ok";
        }

        [WebMethod]
        public List<DocumentItem> GetDocItems(string docName, string docField, string docOffice)
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            List<DocumentItem> objList = new List<DocumentItem>();

            var data = from a in db.Documents
                       orderby a.Doc_ID descending
                       select new
                       {
                           ID = a.Doc_ID,
                           Name = a.Name,
                           Field = a.Field.Name,
                           Office = a.Office.Name,
                           FieldId = a.Field_ID,
                           OfficeId = a.Office_ID
                       };
            if (docName.Trim() != "")
                data = from d in data where d.Name.Contains(docName.Trim()) select d;
            if (docField != "-1")
                data = from d in data where d.FieldId == int.Parse(docField) select d;
            if (docOffice != "-1")
                data = from d in data where d.OfficeId == int.Parse(docOffice) select d;

            //var result = from d in data
            //             select new
            //             {
            //                 ID = d.ID,
            //                 Name = d.Name,
            //                 Field = d.Field,
            //                 Office = d.Office
            //             };
            foreach (var item in data)
            {
                DocumentItem obj = new DocumentItem();
                obj.ID = item.ID;
                obj.Name = item.Name;
                obj.Field = item.Field;
                obj.Office = item.Office;

                objList.Add(obj);
            }

            return objList;
        }
    }

}